var t = require("../../../@babel/runtime/helpers/interopRequireDefault").default,
  e = require("../../../utils/http.js"),
  a = t(require("../../../utils/util")),
  n = new e.HTTP;
let {
  editQdinfo,
  editLxrqd
} = require("../../../api/attract/qd_content")
Page({
  data: {
    selectArray: [{
      id: 1,
      text: "个人级"
    },
    {
      id: 2,
      text: "项目部门级"
    },
    {
      id: 3,
      text: "公司级"
    }
    ],
    khbh: '',
    bID: '',
    purpose: [],
    sort: [],
    sign: [{
      id: 1,
      text: "是"
    },
    {
      id: 2,
      text: "否"
    }
    ],
    nowText: "请选择",
    nowTexta: "请选择",
    nowTextb: "否",
    nowTextf: "请选择",
    qdqy: 2,
    showList: false,
    pickerType: '',
    selectedId: null,
    formData: {
      qymc: '', // 渠道名称
      lxr: '', // 渠道维护人员
      lxfs: '', // 渠道联系人
      hangye: '' // 手机号码
    }
  },

  toggleList(event) {
    const type = event.currentTarget.dataset.type;
    this.setData({
      showList: this.data.pickerType !== type || !this.data.showList,
      pickerType: type
    });
  },

  selectItem(event) {
    const {
      id,
      text,
      type
    } = event.currentTarget.dataset;

    switch (type) {
      case 'qdjb':
        this.setData({
          nowText: text
        });
        break;
      case 'bm':
        this.setData({
          nowTextf: text
        });
        break;
      case 'qdfl':
        this.setData({
          nowTexta: text
        });
        break;
      case 'qdqy':
        this.setData({
          nowTextb: text
        });
        break;
    }

    this.setData({
      selectedId: id,
      showList: false
    });
  },

  formSubmits: function (t) {
    const e = this;
    const o = t.detail.value;

    wx.showModal({
      title: "提示",
      content: "请核对数据是否准确",
      success: function (t) {
        if (t.confirm) {
          console.log(e.data.nowText,
            o.qymc,
            o.lxfs,
            o.lxr,
            e.data.nowTexta)
          if (
            e.data.nowText !== "请选择" &&
            o.qymc &&
            o.lxfs &&
            o.lxr &&
            e.data.nowTexta !== "请选择"
          ) {
            editQdinfo({
              khbh: e.data.khbh,
              qymc: o.qymc,
              bm: e.data.nowTextf,
              qdjb: e.data.nowText,
              whry: o.whry,
              qdfl: e.data.nowTexta,
              qdqy: e.data.nowTextb,
              lxr: o.lxr,
              lxfs: o.lxfs,

            }).then((function (t) {
              editLxrqd({
                khbh: e.data.khbh,
                qymc: o.qymc,
                lxr: o.lxr,
                lxfs: o.lxfs,
              }).then((function (t) { })),
                n.request({
                  url: "https://www.qlsyyun.com:3030/channel/stjd",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    khbh: t.qdbh,
                    qymc: o.qymc,
                    bm: e.data.nowTextf,
                    tname: e.data.name[0]
                  }
                }).then((function (t) { })),
                n.request({
                  url: "https://www.qlsyyun.com:3030/channel/stmx",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    khbh: t.qdbh,
                    qymc: o.qymc,
                    bm: e.data.nowTextf,
                    tname: e.data.name[0],
                    qdjb: e.data.nowText,
                    time: a.default.formatTime(new Date).substring(0, 10)
                  }
                }).then((function (t) { })),
                n.request({
                  url: "https://www.qlsyyun.com:3030/clock/qudao",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    qymc: o.qymc,
                    bm: e.data.nowTextf,
                    zsy: e.data.name[0]
                  }
                }).then((function (t) { })),
                wx.showToast({
                  title: "更新成功",
                  icon: "success",
                  duration: 1e3,
                  success: function () {
                    setTimeout((function () {
                      var pages = getCurrentPages();
                      var beforePage = pages[pages.length - 2];
                      // 假设main()为页面A的数据方法，
                      beforePage.onLoad({
                        qdid: e.data.bID,
                        id: 1
                      });
                      wx.navigateBack({
                        delta: 1,
                      });
                    }), 1e3)
                  }
                })
            }))

          } else {
            wx.showToast({
              title: "请检查是否有必填项未填",
              icon: "none",
              duration: 1000
            });
          }
        }
      }
    });
  },

  quxiao: function () {
    wx.navigateBack({
      delta: 1
    });
  },

  onLoad: function (options) {
    const t = this;

    t.setData({
      name: wx.getStorageSync('user')
    })
    n.request({
      url: "https://www.qlsyyun.com:3030/channel/add",
      data: {
        arr: 1
      }
    }).then((function (e) {
      t.setData({
        purpose: e.data
      })
    })),
      n.request({
        url: "https://www.qlsyyun.com:3030/channel/add",
        data: {
          arr: 2
        }
      }).then((function (e) {
        t.setData({
          sort: e.data
        })
      })).then(() => {
        setTimeout(() => {
          let new_detail = JSON.parse(options.detail);
          // 接收传递的数据
          const {
            qdjb,
            bm,
            qdfl,
            qdqy,
            qymc,
            lxr,
            lxfs,
            whry,
            khbh,
            bID
          } = new_detail
          console.log(new_detail)
          t.setData({
            khbh: khbh,
            bID: bID
          })
          // 回显数据
          t.setData({
            nowText: t.data.selectArray.find(item => item.text == qdjb).text || "请选择",
            nowTextf: t.data.purpose.find(item => item.text == bm).text || "请选择",
            nowTexta: t.data.sort.find(item => item.text == qdfl).text || "请选择",
            nowTextb: t.data.sign.find(item => item.text == qdqy).text || "否",
            selectedId: qdjb || null,
            formData: {
              qymc: qymc || '',
              lxr: lxr || '',
              whry: whry || '',
              lxfs: lxfs || '',

            }
          }); 
        }, 100);
      })

  }
});